﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Reflection;
using System.Windows.Forms;
using FileMonitor.Service.ProcessMonitor.Contract;
using FileMonitor.Service.ProcessMonitor.Model;

namespace FileMonitor.Client
{
    static class Program
    {
        private static Form1 form;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.ToString() == "-c")
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                string action=Console.ReadLine();
              
                while (string.Compare(action, "end", true)!=0)
                {
                    if (string.Compare(action, "form", true) == 0)
                    {
                        if (form == null)
                        {
                            form = new Form1();
                            Application.EnableVisualStyles();
                           // Application.SetCompatibleTextRenderingDefault(false);
                            Application.Run(form);
                        }
                        else
                        {
                            form.Activate();
                        }
                        
                    }
                    else
                    {
                        Excute();
                    }
                    action = Console.ReadLine();
                }
      
            }
        }

        private static void Excute()
        {
            using (ChannelFactory<IMonitorProcessService> channelFactory = new ChannelFactory<IMonitorProcessService>("FileMonitorProcessStateService"))
            {
                IMonitorProcessService channel = channelFactory.CreateChannel();
                Dictionary<string,MonitorProcessInfo> list = channel.GetAllProcessInfo();
                foreach (MonitorProcessInfo mi in list.Values)
                {
                    Console.WriteLine(mi.ProcessName);
                    Console.WriteLine("------------------------------");
                    mi.ProcessedFileInfos.Select(f => f.State).Distinct().ToList().ForEach(s => {
                        Console.WriteLine("-------------------" + s + "----------------------");
                        mi.ProcessedFileInfos.Where(fi => fi.State == s).ToList().ForEach(fi =>
                        {
                            Console.WriteLine(fi.FileName);
                        });
                    });
                }
            }
        }
    }
}
